package com.example.administrator.trains;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.IOException;

/**
 * Created by Shinelon on 2021/3/29.
 */

public class admin_add_ticket extends Activity implements View.OnClickListener {
    private Button addTicketBtn;

    private EditText edt_train_num;
    private EditText edt_start_place;
    private EditText edt_end_place;
    private EditText edt_start_time;
    private EditText edt_end_time;
    private EditText edt_date;
    private EditText edt_type1;
    private EditText edt_type2;
    private EditText edt_type3;
    private EditText edt_type4;
    private EditText edt_type1_price;
    private EditText edt_type2_price;
    private EditText edt_type3_price;
    private EditText edt_type4_price;

    private String train_num;
    private String start_place;
    private String end_place;
    private String start_time;
    private String end_time;
    private String date;
    private String type1;
    private String type2;
    private String type3;
    private String type4;
    private String type1_price;
    private String type2_price;
    private String type3_price;
    private String type4_price;

    Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            if(msg.what==2){
                Toast.makeText(admin_add_ticket.this,"添加失败",Toast.LENGTH_LONG).show();
                admin_add_ticket.this.finish();
            }
            if(msg.what==3){
                Toast.makeText(admin_add_ticket.this,"添加成功",Toast.LENGTH_LONG).show();
            }
            if(msg.what==4){
                Toast.makeText(admin_add_ticket.this,"服务器没有响应，请重新注册",Toast.LENGTH_LONG).show();
            }
            if(msg.what==5){
                Toast.makeText(admin_add_ticket.this,"服务器断开，尝试重连",Toast.LENGTH_LONG).show();
                Socket.connector.releaseSocket();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(getWindow().FEATURE_NO_TITLE);
        setContentView(R.layout.admin_add_ticket);
        addTicketBtn=(Button) findViewById(R.id.btn_add_ticket);
        addTicketBtn.setOnClickListener(admin_add_ticket.this);
        //按下改变颜色，以及执行命令
        addTicketBtn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        v.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape2));
                        break;
                    case MotionEvent.ACTION_UP:
                        v.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape));
                        addTicketBtn.performClick();
                        break;
                }

                return true;
            }
        });

        edt_train_num = (EditText)findViewById(R.id.edt_train_num);
        edt_start_place = (EditText)findViewById(R.id.edt_input_start_place);
        edt_end_place = (EditText)findViewById(R.id.edt_end_place);
        edt_start_time = (EditText)findViewById(R.id.edt_start_time);
        edt_end_time = (EditText)findViewById(R.id.edt_end_time);
        edt_date = (EditText)findViewById(R.id.edt_date);
        edt_type1  = (EditText)findViewById(R.id.edt_type1);
        edt_type2 = (EditText)findViewById(R.id.edt_type2);
        edt_type3 = (EditText)findViewById(R.id.edt_type3);
        edt_type4 = (EditText)findViewById(R.id.edt_type4);
        edt_type1_price  = (EditText)findViewById(R.id.edt_type1_price);
        edt_type2_price = (EditText)findViewById(R.id.edt_type2_price);
        edt_type3_price = (EditText)findViewById(R.id.edt_type3_price);
        edt_type4_price = (EditText)findViewById(R.id.edt_type4_price);
    }

    @Override
    public void onClick(View v) {
        train_num = edt_train_num.getText().toString();
        if (!train_num.matches("[A-Z]{1}[0-9]{2,4}")) {
            Toast.makeText(admin_add_ticket.this,"车次格式不符合要求",Toast.LENGTH_LONG).show();
            return;
        }
        start_place = edt_start_place.getText().toString();
        if (!start_place.matches("[\\u4E00-\\u9FA5]{2,5}")) {
            Toast.makeText(admin_add_ticket.this,"始发地格式不符合要求",Toast.LENGTH_LONG).show();
            return;
        }
        end_place = edt_end_place.getText().toString();
        if (!end_place.matches("[\\u4E00-\\u9FA5]{2,5}")) {
            Toast.makeText(admin_add_ticket.this,"目的地格式不符合要求",Toast.LENGTH_LONG).show();
            return;
        }
        start_time = edt_start_time.getText().toString();
        if (!start_time.matches("^(([0-1]?\\d)|(2[0-4])):[0-5]?\\d$")) {
            Toast.makeText(admin_add_ticket.this,"出发时间格式不符合要求",Toast.LENGTH_LONG).show();
            return;
        }
        end_time = edt_end_time.getText().toString();
        if (!end_time.matches("^(([0-1]?\\d)|(2[0-4])):[0-5]?\\d$")) {
            Toast.makeText(admin_add_ticket.this,"到达时间格式不符合要求",Toast.LENGTH_LONG).show();
            return;
        }
        date = edt_date.getText().toString();
        if (!date.matches("^\\d{4}-0*((1|3|5|7|8|10|12)-0*([1-9]|[1-2]\\d|3[0-1])|(4|6|9|11)-0*([1-9]|[1-2]\\d|30)|2-0*([1-9]|[1-2]\\d))$")) {
            Toast.makeText(admin_add_ticket.this,"日期格式不符合要求",Toast.LENGTH_LONG).show();
            return;
        }
        type1 = edt_type1.getText().toString();
        if (!type1.matches("[0-9]{1,4}")) {
            Toast.makeText(admin_add_ticket.this,"类型一车票数格式不符合要求",Toast.LENGTH_LONG).show();
            return;
        }
        type2 = edt_type2.getText().toString();
        if (!type2.matches("[0-9]{1,4}")) {
            Toast.makeText(admin_add_ticket.this,"类型二车票数格式不符合要求",Toast.LENGTH_LONG).show();
            return;
        }
        type3 = edt_type3.getText().toString();
        if (!type3.matches("[0-9]{1,4}")) {
            Toast.makeText(admin_add_ticket.this,"类型三车票数格式不符合要求",Toast.LENGTH_LONG).show();
            return;
        }
        type4 = edt_type4.getText().toString();
        if (!type4.matches("[0-9]{1,4}") && !type4.equals("无")) {
            Toast.makeText(admin_add_ticket.this,"类型四车票数格式不符合要求",Toast.LENGTH_LONG).show();
            return;
        }
        type1_price = edt_type1_price.getText().toString();
        if (!type1_price.matches("[0-9]{1,4}")) {
            Toast.makeText(admin_add_ticket.this,"类型一车票价格式不符合要求",Toast.LENGTH_LONG).show();
            return;
        }
        type2_price = edt_type2_price.getText().toString();
        if (!type2_price.matches("[0-9]{1,4}")) {
            Toast.makeText(admin_add_ticket.this,"类型二车票价格式不符合要求",Toast.LENGTH_LONG).show();
            return;
        }
        type3_price = edt_type3_price.getText().toString();
        if (!type3_price.matches("[0-9]{1,4}")) {
            Toast.makeText(admin_add_ticket.this,"类型三车票价格式不符合要求",Toast.LENGTH_LONG).show();
            return;
        }
        type4_price = edt_type4_price.getText().toString();
        if (!type4_price.matches("[0-9]{1,4}") && !type4_price.equals("无")) {
            Toast.makeText(admin_add_ticket.this,"类型四车票价格式不符合要求",Toast.LENGTH_LONG).show();
            return;
        }

        switch (v.getId()) {
            case R.id.btn_add_ticket:
                if (Socket.mClient != null) {
                    try {
                        /*发送请求*/
                        Socket.connector.send(Socket.connector.package_data(Socket.connector.TICKET_CMD_TYPE,
                                Socket.connector.ADD_TICKET, "00",
                                train_num + CommunicationData.empty +
                                        start_place + CommunicationData.empty +
                                        end_place + CommunicationData.empty +
                                        start_time + CommunicationData.empty +
                                        end_time + CommunicationData.empty +
                                        date + CommunicationData.empty +
                                        type1 + CommunicationData.empty +
                                        type2 + CommunicationData.empty +
                                        type3 + CommunicationData.empty +
                                        type4 + CommunicationData.empty +
                                        type1_price + CommunicationData.empty +
                                        type2_price + CommunicationData.empty +
                                        type3_price + CommunicationData.empty +
                                        type4_price + CommunicationData.empty));
                    /*接收服务端是否成功接收信息的ack*/
                        Socket.connector.recv_ack();
                        if (CommunicationData.ack == "01") {
                            //添加成功
                            Socket.connector.recv_ack();
                            if (CommunicationData.ack == Socket.connector.ADD_TICKET_SUCCESS_ACK) {
                                Message m = new Message();
                                m.what = 3;
                                myHandler.sendMessage(m);
                            }
                            if (CommunicationData.ack == Socket.connector.ADD_TICKET_FALSE_ACK) {
                                //添加失败
                                Message m = new Message();
                                m.what = 2;
                                myHandler.sendMessage(m);
                            }

                            this.finish();
                        } else {
                            Message m = new Message();
                            m.what = 4;
                            myHandler.sendMessage(m);
                        }

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else {
                    Message m = new Message();
                    m.what = 5;
                    myHandler.sendMessage(m);
                }
                break;
            default:
                break;
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
